﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace MyRecipeBook.Classes
{
    public class Measurements
    {

        public Status GetMeasurementType(int id, out MeasurementType type)
        {
            type = MeasurementType.Unknown;

            if (id == 0) return new Status { Result = StatusResult.Error, Code = StatusCode.ConversionToTypeIsInvalid };

            try
            {
                const string sql = "SELECT type FROM measurements WHERE id=@id";
                var conn = new SqlConnection(Properties.Settings.Default.MyRecipeBookDbCS);
                var comm = new SqlCommand { CommandText = sql, Connection = conn };

                comm.Parameters.Add("id", System.Data.SqlDbType.Int);
                comm.Parameters["id"].Value = id;

                conn.Open();

                var reader = comm.ExecuteReader();

                if (reader != null)
                {
                    if (reader.HasRows)
                    {
                        if (reader.Read())
                        {
                            type = (MeasurementType) short.Parse(reader["type"].ToString());
                        }
                    }
                    else
                    {
                        return new Status { Result = StatusResult.Failed, Code = StatusCode.MeasurementNotFound };
                    }

                    reader.Close();
                }
                conn.Close();
            }
            catch
            {
                return new Status { Result = StatusResult.Exception, Code = StatusCode.Unknown };
            }

            return new Status { Result = StatusResult.Success, Code = StatusCode.MeasurementTypeFound };
        }

        public Status GetMeasurementId(string name, out int id)
        {
            id = -1;
            if (id == 0) return new Status { Result = StatusResult.Error, Code = StatusCode.ConversionToTypeIsInvalid };
            var s = new Status();

            try
            {
                const string sql = "SELECT id FROM measurements WHERE name=@name";
                var conn = new SqlConnection(Properties.Settings.Default.MyRecipeBookDbCS);
                var comm = new SqlCommand { CommandText = sql, Connection = conn };

                comm.Parameters.Add("name", System.Data.SqlDbType.Int);
                comm.Parameters["name"].Value = name;

                conn.Open();

                var reader = comm.ExecuteReader();

                if (reader != null)
                {
                    if (reader.HasRows)
                    {
                        if (reader.Read())
                        {
                            id = int.Parse(reader["name"].ToString());
                            s = new Status { Result = StatusResult.Success, Code = StatusCode.MeasurementFound };
                        }
                    }
                    else
                    {
                        s = new Status { Result = StatusResult.Failed, Code = StatusCode.MeasurementNotFound };
                    }

                    reader.Close();
                }
                conn.Close();
            }
            catch
            {
                s = new Status { Result = StatusResult.Exception, Code = StatusCode.Unknown };
            }

            return s;
        }
    }

    public enum MeasurementType : short
    {
        Weight = 1, // WEIGHT (e.g. Gram, Ounce, Kilogram)
        Type = 2, // TYPE (e.g. %, Bars)
        Quantity = 3, // Quantity
        Unknown = 4// UNKNOWN
    }
}
